package cn.e4j.modular.api.controller;

import cn.digiwin.wx.utils.WeixinHandler;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.GwaService;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.api.service.Gwat510Service;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单运输信息审核 控制类
 * Created by shixc on 2018/5/7.
 */
@RestController
@RequestMapping("api/wx/gwat510")
public class Gwat510Controller extends ControllerSupport {
    private static Logger logger = LoggerFactory.getLogger(Gwat510Controller.class);

    @Autowired
    Gwat510Service gwat510Service;

    @Autowired
    private GwaService gwaService;

    @Autowired
    private Gwat200Service gwat200Service;

    /**
     * 获取订单运输信息
     *
     * @return
     */
    @RequestMapping(value = "getGwcuData")
    public ResponseData getGwcuData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser currentUser = wapper.getSysUser();
        try {
            // 获取区域权限-进行省市过滤数据
            inDto.put("GWAA003", currentUser.getACCOUNT());
            inDto.put("MMRE004S", gwat200Service.getMmreData(inDto));

            list = gwat510Service.getGwcuData(wapper.getPage(), inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 判断是否可以取消确认--单笔
     *
     * @return
     */
    @RequestMapping(value = "getGwcw")
    public ResponseData getGwcw(@DtoParam DtoWapper wapper) {
        Dto inDto = JSONObject.parseObject(wapper.getDto().getString("obj"), Dto.class);
        try {
            gwat510Service.getGwcw(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 判断是否可以取消确认--批量
     *
     * @return
     */
    @RequestMapping(value = "getGwcwlist")
    public ResponseData getGwcwlist(@DtoParam DtoWapper wapper) {
        List<Dto> items = JSONObject.parseArray(wapper.getDto().getString("items"), Dto.class);
        try {
            for(Dto item : items){
                gwat510Service.getGwcw(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 确认订单运输信息--单笔
     *
     * @return
     */
    @RequestMapping("updateByY")
    public ResponseData updateByY(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto inDto1 = JSONObject.parseObject(wapper.getDto().getString("obj"), Dto.class);

        inDto1.put("ENT", inDto.getString("ENT"));
        try {
            String result = gwat510Service.check(inDto1);
            if (ToolUtil.isEmpty(result)) {
                gwat510Service.updateDataYSingle(inDto1, wapper.getSysUser());
            } else {
                return new ErrorResponseData(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        try{
            sendMsg(inDto1.getString("GWCU015"), inDto1.getString("GWCUDOCNO"), inDto1.getString("GWCU009"),
                    inDto1.getString("GWCU008"), inDto1.getString("GWCU010"));
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 确认订单运输信息 -- 批量
     *
     * @param wapper
     * @return
     */
    @RequestMapping("updateByYlist")
    @ResponseBody
    public ResponseData updateByYlist(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> items = JSONObject.parseArray(wapper.getDto().getString("items"), Dto.class);
        try {
            for(Dto item : items){
                item.put("ENT", inDto.getString("ENT"));
                String result = gwat510Service.check(item);
                if (!ToolUtil.isEmpty(result)) {
                    return new ErrorResponseData(result);
                }
            }
            gwat510Service.updateDataYBatch(items, wapper.getSysUser());
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器出小差了");
        }
        try{
            for(Dto item : items){
                sendMsg(item.getString("GWCU015"), item.getString("GWCUDOCNO"), item.getString("GWCU009"),
                        item.getString("GWCU008"), item.getString("GWCU010"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new SuccessResponseData();
    }

    /**
     * 取消确认订单运输信息--单笔
     *
     * @return
     */
    @RequestMapping("updateByN")
    public ResponseData updateByN(@DtoParam DtoWapper wapper) {
        Dto inDto = JSONObject.parseObject(wapper.getDto().getString("obj"), Dto.class);
        try {
            gwat510Service.updateDataNSingle(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器出小差了");
        }
        return new SuccessResponseData();
    }

    /**
     * 取消确认订单运输信息 -- 批量
     *
     * @param wapper
     * @return
     */
    @RequestMapping("updateByNlist")
    @ResponseBody
    public ResponseData updateByNlist(@DtoParam DtoWapper wapper) {
        List<Dto> items = JSONObject.parseArray(wapper.getDto().getString("items"), Dto.class);
        try {
            gwat510Service.updateDataNBatch(items, wapper.getDto());
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务器出小差了");
        }
        return new SuccessResponseData();
    }

    /**
     * 发送消息
     *
     * @param phone
     * @param docno
     * @param driver
     * @param license
     * @param mobile
     */
    private void sendMsg(String phone, String docno, String driver, String license, String mobile) {
        List<String> mobileList = new ArrayList<>();
        //经销商
        if (ToolUtil.isNotEmpty(phone)) {
            mobileList.add(phone);
        }
        //线人
        Dto gwcsIn = Dtos.newDto();
        Dto gwcsOut = gwat510Service.getGwcsXR(gwcsIn);

        if (ToolUtil.isNotEmpty(gwcsOut) && ToolUtil.isNotEmpty(gwcsOut.getString("GWCS007"))) {
            mobileList.add(gwcsOut.getString("GWCS007"));
        }
        if (mobileList.size() > 0) {
            Dto gwaaDto = Dtos.newDto();
            gwaaDto.put("GWAAACTI", "Y");
            gwaaDto.put("GWAA004S", mobileList);
            List<Dto> sysUser = gwaService.getGwaUserByMobiles(gwaaDto);
            WeixinHandler.sendMsg(sysUser,
                    String.format("订单运输信息[%s]：司机[%s], 车牌[%s], 手机号[%s]",
                            docno, driver, license, mobile)
            );
        }
    }
}
